﻿#ifndef WIFIMANAGER_H
#define WIFIMANAGER_H

#include <windows.h>
#include <wlanapi.h>
#include <wincrypt.h>
#include <iostream>
#include <vector>
#include <functional>
#include <Iphlpapi.h>
#include <TlHelp32.h>
#include <processthreadsapi.h>
#pragma comment(lib, "wlanapi.lib")
#pragma comment(lib, "crypt32.lib")
#pragma comment(lib,"Iphlpapi.lib")
#pragma comment(lib, "advapi32.lib")

//WiFiInfo结构体，用来保存获取的热点信息
struct WiFiInfo {
    // 使用 std::wstring 替代 CString 来存储 SSID
    std::wstring cstrSSID;
    // 使用 std::wstring 替代 CString 来存储密码
    std::wstring cstrPassword;
    // 连接状态
    bool bLinked;
    // 信号强度值
    int nSignalValue;
    // 使用  LPWSTR 来存储配置文件 XML 内容
    LPWSTR  pProfileXml;
    //手动连接还是自动连接,0为自动连接,1为手动连接
    bool connectionMode;
    //是否启用密码加密,默认启用
    bool is_encrypt;
    //加密类型
    DOT11_BSS_TYPE  dot11BssType;
    //加密算法
    DOT11_AUTH_ALGORITHM dot11DefaultAuthAlgorithm;

    DOT11_CIPHER_ALGORITHM  dot11DefaultCipherAlgorithm;
    // 构造函数，对成员变量进行初始化
    WiFiInfo() : cstrSSID(L""), cstrPassword(L""), bLinked(false), nSignalValue(0), pProfileXml(NULL), connectionMode(false), is_encrypt(true)
    {
        dot11BssType = dot11_BSS_type_infrastructure;
        dot11DefaultAuthAlgorithm = DOT11_AUTH_ALGO_WPA;

    }
};
struct NetWork_Card_Info
{
    char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4];
    char IP[4 * 4];
    char Subnet_mask[4 * 4];
    char macStr[18];

};
typedef std::vector<WiFiInfo> WiFiInfoList;
typedef std::vector<NetWork_Card_Info> NetWork_Card_Info_List;
typedef std::vector<GUID> InterfaceInfo_List;

using CONNECT_NOTIFICATION_CALLBACK = std::function<void(PWLAN_CONNECTION_NOTIFICATION_DATA, DWORD)>;

class WiFiManager
{
    friend void OnNotificationCallback(PWLAN_NOTIFICATION_DATA data, PVOID context);
public:
    static WiFiManager& GetInstance()
    {
        static WiFiManager instance;
        return instance;
    }
    bool GetWlanInterfaces(InterfaceInfo_List& interfaceInfo_list);
    bool RigisterConnectNotification(CONNECT_NOTIFICATION_CALLBACK funcCallback);//注册连接结果回调
    bool ScanWiFi(GUID& guid); //扫描无线网卡wifi热点
    bool GetWiFiList(WiFiInfoList& wifilist, GUID& guid); //获取wifilist
        bool ConnectWiFi(WiFiInfo wifiinfo, std::wstring targetKey, GUID guid,bool is_manual = false); //连接指定的ssid
    bool DisconnectWiFi(GUID guid);
    bool Set_WiFi_Status(bool status, GUID guid);

private:
    //构造函数，返回一个实例化的静态对象
    WiFiManager()
        : m_wlanHandle(NULL),
        m_funcConnectNotification(NULL)
    {
    }
    //初始化WiFi句柄
    bool InitialHandle();
    //设置配置文件
    bool SetProfile(WiFiInfo& wifiinfo, const std::wstring& targetKey, const GUID& interfaceGuid, bool is_manual);
    //设置连接结果
    bool SetConnectResult(PWLAN_CONNECTION_NOTIFICATION_DATA pData, unsigned long dwConnectCode);

    bool ImpersonateLoginUser();

    DWORD GetProcessID(const wchar_t* szName);

    bool SetPrivilege(const wchar_t* szPrivilege);
private:
    //设置句柄
    HANDLE m_wlanHandle;
    CONNECT_NOTIFICATION_CALLBACK m_funcConnectNotification;

};
#endif // WIFIMANAGER_H
